<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>角色分配</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" href="<%=path%>/lib/bootstrap/css/bootstrap.min.css" />
	<link rel="stylesheet" type="text/css" href="<%=path%>/css/xcConfirm.css"/>
	<script type="text/javascript" src="<%=path %>/lib/jquery-1.8.1.min.js"></script>
	<script type="text/javascript" src="<%=path %>/lib/bootstrap/js/bootstrap.min.js"></script>
	<script src="<%=path%>/js/xcConfirm.js" type="text/javascript" charset="utf-8"></script>
  </head>
  
  <body>
  <h2 style="padding: 3%;">角色分配</h2>
    <div class="container">
	<div class="row clearfix">
	<div class="col-md-12 column">
			<input type="text" class="search-query" name="" placeholder="输入角色类型 / 角色名称" style="height: 3%;"/>
			<input type="button" class="btn btn-primary" name="" value="查询"/>
		<div class="col-md-12 column">
			<table class="table table-hover">
				<thead>
					<tr>
						<th class="">
							用户姓名
						</th>
						<th>
							状态
						</th>
						<th>
							拥有的角色
						</th>
						<th>
							操作
						</th>
					</tr>
				</thead>
				<tbody>
				
				<c:forEach items="${userrole }" var="role" varStatus="col">
					<tr class="${col.count%2==0?'':'info' }">
						<td>
							${role.user.name }
						</td>
						<td>
							有效
						</td>
						<td>
							${role.role.roleName }
						</td>
						<td>
							<a  class="btn btn-warning allot" onclick="getId(${role.user.userId})">授予</a>
							<a  class="btn btn-danger revoke" onclick="getId(${role.user.userId})">撤销</a>
						</td>
					</tr>
					</c:forEach>
				</tbody>
			</table>
		</div>
	</div>
		 <ul class="pager">
						  <li class="previous ${pageNo==1?'disabled':''}">
							  <c:if test="${pageNo==1}">
							  	<a>上一页</a>
							  </c:if>
							  <c:if test="${pageNo!=1}">
							  	<a href="userRoles.action?pageNo=${pageNo-1 }">上一页</a>
							  </c:if>
						  </li>
						  
						  <li style="text-align: center;">${pageNo}</li>
						  
						  <li class="next ${userrole.size()%pageSize!=0?'disabled':''}">
							  <c:if test="${userrole.size()%pageSize!=0 || userrole.size()==0} ">
							  		<a>下一页</a>
							  </c:if>
							  <c:if test="${userrole.size()%pageSize==0 && userrole.size()!=0}">
							  		<a href="userRoles.action?pageNo=${pageNo+1 }">下一页</a>
							  </c:if>
						  </li>
						  
						  <!-- previous disabled显示禁用 -->
					</ul> 
</div>
<script type="text/javascript">
var userId = null;
$(function(){
	 $(".allot").click(function(){
		 //提示信息
		 var roleTitle=  "将授予这个用户新角色:";
		 //下拉框开始
		 var roleTxt = "<select name='role.roleName' id='roleAddName'>"+"<option value='0'>"+"请选择"+"</option>";
		 //获取下拉框的值
		 $.ajax({
             type : 'POST',
             url : "roles.action",
             data:{"userId":userId,"choose":"add"},
             async:false,
             success : function(data) {
					 for (var i = 0; i < data.length; i++) {
						 //将List数据放入下拉框
						 roleTxt += "<option  value='"+data[i].roleId+"'>"+data[i].roleName+"</option>";
					}
             },
             error : function(data) {
             	 window.wxc.xcConfirm("修改失败", window.wxc.xcConfirm.typeEnum.error);
             }
    });
		 	//下拉框结束
		 	roleTxt += "</select>";
		 window.wxc.xcConfirm(roleTitle+roleTxt, window.wxc.xcConfirm.typeEnum.confirm,{
						onOk:function(v){
							console.log(v);
								var roleId = $("#roleAddName").val();
								if(roleId==0){
									 window.wxc.xcConfirm("至少选择一项!", window.wxc.xcConfirm.typeEnum.error);
								}
								var userRole ={"userId":userId,"roleId":roleId};
								 		$.ajax({
				                        type : 'POST',
				                        url : "insertUserRole.action",
				                        data : userRole,
				                        async:false,
				                        success : function(data) {
				                        	 if(data=="yes"){
												 window.location="userRoles.action";  
											 } 
				                        },
				                        error : function(data) {
				                        	 window.wxc.xcConfirm("修改失败", window.wxc.xcConfirm.typeEnum.error);
				                        }
				                });
				 		}
					});
	 });
	 //撤销角色
	 $(".revoke").click(function(){
		//提示信息
		 var roleTitle=  "将撤销这个用户已有角色:";
		 //下拉框开始
		 var roleTxt = "<select name='role.roleName' id='revokeUserRoleId'>"+"<option value='0'>"+"请选择"+"</option>";
		 //获取下拉框的值
		 $.ajax({
             type : 'POST',
             url : "roles.action",
             data:{"userId":userId,"choose":"revoke"},
             async:false,
             success : function(data) {
					 for (var i = 0; i < data.length; i++) {
						 //将List数据放入下拉框
						 roleTxt += "<option  value='"+data[i].roleId+"'>"+data[i].roleName+"</option>";
					}
             },
             error : function(data) {
             	 window.wxc.xcConfirm("撤销失败", window.wxc.xcConfirm.typeEnum.error);
             }
    });
		 	//下拉框结束
		 	roleTxt += "</select>";
		 	window.wxc.xcConfirm(roleTitle+roleTxt, window.wxc.xcConfirm.typeEnum.confirm,{
				onOk:function(v){
					console.log(v);
						var roleId = $("#revokeUserRoleId").val();
						if(roleId==0){
							 window.wxc.xcConfirm("至少选择一项!", window.wxc.xcConfirm.typeEnum.error);
							 return;
						}
						var userRole ={"userId":userId,"roleId":roleId};
						 		$.ajax({
		                        type : 'POST',
		                        url : "revokeRole.action",
		                        data : userRole,
		                        async:false,
		                        complete:function(data){
		                        	 if(data.responseText=="yes"){
										 window.location="userRoles.action";  
									 } else{
										 window.wxc.xcConfirm("撤销失败", window.wxc.xcConfirm.typeEnum.error);
									 }
		                        }
		                });
		 		}
			});
	 });
});
//获取用户Id
function getId(data){
	userId = data;
};
</script>
<script src="js/canvas-nest.min.js" count="200" zindex="-2" opacity="0.5" color="47,135,193" type="text/javascript"></script>
  </body>
</html>
